package edu.usc.csci538.hw6.data;

import java.util.Calendar;
import java.util.Date;


public class CalendarEntry {

    public static CalendarEntry getDefaultEntry() {
        CalendarEntry entry = new CalendarEntry();
        entry.repeat = CalendarEntry.REPEAT_NONE;
        
        Calendar cal = Calendar.getInstance();
        entry.startTime = new Date(cal.getTime().getTime() - 8*60*60*1000L);
        entry.endTime = new Date(entry.startTime.getTime());
        
        return entry;
    }
    private String subject;
    private String location;
    private Date startTime;
    private Date endTime;
    private boolean enableAlarm;
    private int repeat;
    public static final int REPEAT_NONE = 0;
    public static final int REPEAT_DAILY = 1;
    public static final int REPEAT_WEEKLY = 2;
    public static final int REPEAT_MONTHLY = 3;
    
    /**
     * To prepopulate values, do something like this
     * CalendarEntry entry = new CalendarEntry();
     * entry.setSubject("Meeting").setLocation("Library")
     *      .setDate(2008, 3, 3).setStartTime(5, 0, false)
     *      .setEndTime(6, 0, false);
     */
    public CalendarEntry() {
        subject = "";
        location = "";
        startTime = new Date(0);
        endTime = new Date(0);
        
        enableAlarm = false;
        repeat = REPEAT_NONE;
    }
    
    // Getters
    public String getSubject() {
        return subject;
    }
    public String getLocation() {
        return location;
    }
    public boolean alarmEnabled() {
        return enableAlarm;
    }
    public int getRepeat() {
        return repeat;
    }
    private int getCalendarField(Date date, int field) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(field);
    }
    public int getYear() {
        return getCalendarField(startTime, Calendar.YEAR);
    }
    public int getMonth() {
        return getCalendarField(startTime, Calendar.MONTH);
    }
    public int getDate() {
        return getCalendarField(startTime, Calendar.DATE);
    }
    public boolean isStartTimeAM() {
        return getCalendarField(startTime, Calendar.AM_PM) == Calendar.AM;
    }
    public int getStartTimeHour() {
        return getCalendarField(startTime, Calendar.HOUR);
    }
    public int getStartTimeMinute() {
        return getCalendarField(startTime, Calendar.MINUTE);
    }
    public boolean isEndTimeAM() {
        return getCalendarField(endTime, Calendar.AM_PM) == Calendar.AM;
    }
    public int getEndTimeHour() {
        return getCalendarField(endTime, Calendar.HOUR);
    }
    public int getEndTimeMinute() {
        return getCalendarField(endTime, Calendar.MINUTE);
    }
    
    // Setters
    public CalendarEntry setSubject(String subject) {
        this.subject = subject;
        return this;
    }
    public CalendarEntry setLocation(String location) {
        this.location = location;
        return this;
    }
    public CalendarEntry setAlarmEnabled(boolean val) {
        enableAlarm = val;
        return this;
    }
    public CalendarEntry setRepeat(int val) {
        if (val < 0 || val > 3) {
            throw new RuntimeException("Bad repeat value");
        }
        repeat = val;
        return this;
    }
    public CalendarEntry setDate(int year, int month, int date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DATE, date);
        startTime = cal.getTime();
        return this;
    }
    private Date updateTime_Internal(Date time, boolean isAM, int hour, int min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.set(Calendar.AM_PM, isAM ? Calendar.AM : Calendar.PM);
        cal.set(Calendar.HOUR, hour);
        cal.set(Calendar.MINUTE, min);
        return cal.getTime();
    }
    public CalendarEntry setStartTime(int hour, int min, boolean isAM) {
        startTime = updateTime_Internal(startTime, isAM, hour, min);
        return this;
    }
    public CalendarEntry setEndTime(int hour, int min, boolean isAM) {
        endTime = updateTime_Internal(startTime, isAM, hour, min);
        return this;
    }

}
